// app.js
const SERVER = require("./interfaces/interface")
const VEH = require("./interfaces/vehicles")
const ORDER = require("./interfaces/order")
let inter = undefined

App({
  async onLaunch() {
    SERVER.init_http_head()
    this.refreshAccountInfo()
  },

  globalData: {
    account_info: {
      nickName: "Example",
      account: "need2login@example.com",
      avatar: "https://i2.hdslb.com/bfs/face/61b39bcde88be469dfd1beb8e8b33858eda401f6.jpg"
    },
  },

  async startCyclicQueryOrder(){
    let success = await ORDER.refreshCurrentOrder()
    inter = setInterval(()=>{
      ORDER.refreshCurrentOrder()
    },1000)
    return success
  },

  async refreshAccountInfo(){
    if(SERVER.has_login())
    {
      let result = await SERVER.queryUserInfo()
      if(result.status=="success")
      {
        let info = result.res
        this.setAccountInfo({
          account: info.account,
          nickName: info.name,
        })
      }
      else{
        this.setAccountInfo({
          account: "error@error.com",
          nickName: "服务器异常"
        })
      }
      let res_veh = await VEH.refreshVehicles()
      let res_order = await ORDER.refreshOrders()
      console.log("查询状态",result.status=="success",res_veh,res_order)
      if(!(result.status=="success"&&res_veh&&res_order))
      {
        wx.showModal({
          title:"当前服务器异常",
          content:"是否尝试更换服务器地址",
          success:(res)=>{
            if(res.confirm)
            {
              this.changeServer()
            }
          }
        })
      }
    }
  },

  setAccountInfo(infos) {
    for (let info in infos) {
      if (info in this.globalData.account_info) {
        this.globalData.account_info[info] = infos[info]
      }
    }
  },

  getAccountInfo() {
    return {
      ...this.globalData.account_info
    }
  },

  loginJump() {
    if (SERVER.has_login())
      return
    wx.showActionSheet({
      itemList: ['注册', '登录'],
      success(res) {
        if (res.tapIndex == 0) {
          wx.navigateTo({
            url: '../login/login?action=register',
          })
        } else {
          wx.navigateTo({
            url: '../login/login?action=login',
          })
        }

      },
      fail(res) {
        console.log(res.errMsg)
        return -1
      }
    })
  },

  logout() {
    this.globalData.account_info = {
      nickName: "Sample",
      account: "need2login@example.com",
      avatar: "https://i2.hdslb.com/bfs/face/61b39bcde88be469dfd1beb8e8b33858eda401f6.jpg"
    }
    ORDER.clear()
    VEH.clear()
  },

  async changeServer(){
    let http_head
    try{
      http_head = await new Promise(function(resolve,reject){
        wx.showModal({
          title: "新服务器地址",
          editable: true,
          success: (res) => {
            if (res.confirm) {
              resolve(res.content)
            }
            else reject("cancel")
          },
          fail: () => {
            reject("fail")
          }
        })
      })
      SERVER.change_http_head(http_head)
    }catch{
      return
    }
  },
})